<?php

namespace App\Admin\Controllers;

use App\Models\Car;
use App\Models\Config;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\MessageBag;

class ConfigController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('配置管理');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('编辑配置');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Config::class, function (Grid $grid) {
            
            $grid->name('配置项');
            
            $grid->disableCreateButton();
            $grid->actions(function($grid){
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Config::class, function (Form $form) {
    
            $form->tab('抢车位',function($form){
                $form->embeds('cfgStopLimit','车位参数(分钟)',function($form){
                    $form->slider('stop','停车冻结时间')->options(['max' => 2*60, 'min' => 1, 'step' => 1, 'postfix' => '分钟']);
                    $form->slider('stopOneParking','同一玩家下停车冻结时间')->options(['max' => 2*60, 'min' => 1, 'step' => 1, 'postfix' => '分钟']);
                    $form->slider('maxStopTime','停车最大时长')->options(['max' => 10, 'min' => 1, 'step' => 1, 'postfix' => '小时']);
                    $form->slider('maxProtectTime','保护期最大时长')->options(['max' => 8*30, 'min' => 30, 'step' => 30, 'postfix' => '分钟']);
                    $form->slider('maxDriveTime','违章停车最大收费时长')->options(['max' => 24*5, 'min' => 5, 'step' => 1, 'postfix' => '小时']);
                    $form->number('DriveCredit','违章停车每分钟扣除金额');
                    $form->divide();
                    $form->rate('ParkingFreePersent','免费车位收益百分比');
                    $form->rate('ParkingPrivatePersent','私家车位收益百分比');
                    $form->rate('ParkingChargePersent','新能源车位收益百分比');
                    $form->rate('ParkingChargeCarPersent','新能源车位停放新能源车时收益百分比');
                    $form->divide();
                    $form->rate('PunishedPersent','被贴条收益所得百分比');
                    $form->rate('PunisherPersent','贴条者收益所得百分比');
                    $form->divide();
                    $form->currency('upgradeParkingPrivate','升级为私家车位所需金币')->symbol('￥');
                    $form->currency('upgradeParkingCharge','升级为充电车位所需金币')->symbol('￥');
                    $form->divide();
                    $form->currency('GoldForNewUser','新用户赠送金币')->symbol('￥');
                    $cars = Car::withoutGlobalScopes()->get(['CarID','CarName','CarPrice']);
                    $option_cars=[];
                    foreach($cars as $car){
                        $option_cars[$car->CarID]=sprintf('%s(￥%s)',$car->CarName,$car->CarPrice);
                    }
                    $form->select('CarForNewUser','新用户赠送车辆')->options($option_cars);
                });
                $form->embeds('levels','车库解锁配置',function($form){
                    for ($i=1;$i<=10;$i++){
                        $form->hidden('car_limit_'.$i,'车库'.$i.'车辆限制');
                        $form->currency('gold_'.$i,'第'.$i.'个车库解锁金额')->symbol('￥');
                    }
                });
            })->tab('抽卡配置',function($form){
                $form->embeds('draw_lots_config','',function($form){
                    $form->number('draw_lots_num','每日抽卡次数');
                });
            })->tab('神秘商人',function($form){
                $form->embeds('merchant_config','',function($form){
                    $form->slider('begin_hour','每日抽奖开始时间')->options(['max' => 23, 'min' => 0, 'step' => 1, 'postfix' => '点']);
                    $form->slider('end_hour','每日抽奖结束时间')->options(['max' => 24, 'min' => 1, 'step' => 1, 'postfix' => '点']);                    $form->number('day_limit','每日次数限制');
                    $form->rate('lottery_rate','每次中奖概率');
                    $form->number('gold','抽奖所需金币');
                });
            })->tab('接口配置',function($form){
                $form->embeds('api_config','',function($form){
                    $form->text('host','用户接口域名')->default('');
                    $form->text('score_host','积分接口域名')->default('');
                    $form->text('social_host','社交接口域名')->default('');
                    $form->text('action_code_incre','增加积分动作码');
                    $form->text('action_code_incre_sign','签到增加积分动作码');
                    $form->text('action_code_incre_card','卡片集齐增加积分动作码');
                    $form->switch('remote_follow_switch','使用社区好友关系')->default(false);
                    $form->number('demo_user_id','应用内默认模拟用户ID')->default(1)->help('测试期使用');
                });
            });
            
    
            $form->saved(function ($form) {
                if(!is_writable(storage_path('app/car_config.json'))){
                    $error = new MessageBag([
                        'title'   => 'storage/app/car_config.json文件不可写，保存失败',
                    ]);
                    return back()->with(compact('error'));
                }
                //将文件存至本地
                if(Storage::put('car_config.json',$form->model()->toJson())){
                    $success = new MessageBag([
                        'title'   => '保存成功',
                    ]);
                    return back()->with(compact('success'));
                }else{
                    $error = new MessageBag([
                        'title'   => '保存失败',
                    ]);
                    return back()->with(compact('error'));
                }
                
                
            });
        });
    }
}
